home *** CD-ROM | disk | FTP | other *** search
- Path: EU.net!sun4nl!ittpub!ittpub!nntp
- Newsgroups: comp.lang.c++
- Subject: Re: Exceptions and operator new. Help
- Message-ID: <1996Feb16.174250.1777@ittpub>
- From: wil@ittpub.nl (Wil Evers)
- Date: 16 Feb 96 17:42:50 WET
- References: <4fu14a$2vg@news.tamu.edu>
- Distribution: world
- Nntp-Posting-Host: lintilla
-
- In article <4fu14a$2vg@news.tamu.edu> tpradeep@cs.tamu.edu (Pradeep K
- Tapadiya) writes:
-
- > According to the C++ specs, a new will throw a xalloc exception
- > if memory allocation fails.
-
- According to the draft standard, it throws a bad_alloc exception.
-
- > The return value from new need not be
- > NULL. In addition, the constructor can throw a different exception
- > as defined by you, the programmer. This means, each time, I do a
- > new, I have to do something like
- >
- > myObject* p = NULL;
- >
- > try {
- > myObject* p = new myObject;
- > }
- > catch (xlloc e) {
- > // do not delete p here since it was never allocated
- > ...
- > }
- > catch (myException& e) {
- > delete p;
- > ...
-
- No. The memory allocated will be released when the constructor code throws
- an exception as well.
-
- > Moreover, if myObject's destructor has to do some
- > memroy cleanup, for example,
- >
- > myObject::~myObject ()
- > {
- > delete [] m_pVarList;
- > }
- >
- > then, the constructor will probably has to do something like
- >
- >
- > myObject::myObject ()
- > {
- > try {
- > m_pVarList = new char [10];
- > }
- > catch (xalloc e) {
- > m_pVarList = NULL; // explicitly set it to NULL as destructor will
- > // try to delete a bogus pointer
- > throw xalloc ();
- > }
- > }
-
- You don't normally have to do this. Assuming your code looks like:
-
- void f()
- {
- myObject *p = new myObject;
- // do things with *p
- delete p;
- }
-
- Then, if an exception is thrown in allocating or constructing the new
- object and f() doesn't attempt to catch it, the delete statement is never
- reached. Instead, control will be passed directly to the latest try block
- entered that wants to catch the exception thrown.
-
- > Now, setnewHandler adds another variable to the model. As you cannot
- > rely on what third party library is doing, anytime you do a new on
- > your object, you will explictly have to do a setnewhandler to your
- > handler.
-
- A quality library probably can be trusted to either install a reasonable
- new_handler or better yet, only use its own new_handler for its own
- dynamic allocation requests.
-
- > Though we have been programming in C++ for quite some time, this is
- > the first time our company is venturing out into the world of
- > exceptions. It now appears exceptions would make the development process
- > more complicated.
-
- True, exception handling is controversial. But not for the reasons you
- think it is. The real caveats are far more subtle. Exceptions are not an
- easy subject, and you should probably make sure you understand the
- exception mechanism a bit better before you start using them.
-
- In the mean time, you can use the overloaded operator new that takes an
- extra argument of type nothrow. This version will not throw a bad_alloc
- exception on allocation failure, but return 0 instead.
-
- Good luck and best regards,
-
- - Wil
-
-